package keystore;

import java.rmi.Naming;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Date;
import java.util.Random;

public class Client extends UnicastRemoteObject implements IClient {
	
	private static final long serialVersionUID = 1L;
	
	private static long startTime = 0;

	
	public Client() throws RemoteException {
		
	}

	/**
	 * @param args
	 * @throws RemoteException 
	 */
	public static void main(String[] args) throws RemoteException {
		
		String port = Integer.toString(Registry.REGISTRY_PORT);
		try {
			System.setSecurityManager( new RMISecurityManager() );
			Client client = new Client();
			Naming.rebind("//localhost:" + port + "/" + IClient.SERVICENAME, client);
			System.out.println("Bound RMI object in local registry at port " + port);
		} catch (Exception ex) {
			ex.printStackTrace();
		}		
				
		NodeFactory nf = new NodeFactory();
		
		startTime = new Date().getTime();
		nf.searchLeaf(200, "some data...");
		
		startTime = new Date().getTime();
		nf.searchLeaf(201, "some data...");
		
		startTime = new Date().getTime();
		nf.searchLeaf(1600, "some data...");
		
		startTime = new Date().getTime();
		nf.searchLeaf(3500, "some data...");		
//		
		
//		Random r = new Random();
//		
//		for (int i=0;i<1000;i++) {
//			double val = r.nextGaussian() * 2000 + 15000;
//			int key = (int) Math.round(val);
//			startTime = new Date().getTime();
//			nf.searchLeaf(key, "some data...");
//		}
				


	}

	@Override
	public void operationFinished(boolean success) throws RemoteException {
		if (success) {
			long endTime = new Date().getTime();			
			
			System.out.println("Op. completed.");
			System.out.println("Time needed for operation: " +(endTime - startTime) +" ms");	
		}
		else {
			System.err.println("Something doesn't worked out well...");
		}		
	}

}
